package com.ajl.wx.config.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * 微信HttpMessageConverter，由于RestTemplate默认不支持text/plain的转换，所以需要在这里添加
 */
public class WechatHttpMessageConverter extends AbstractHttpMessageConverter<Map> {

    public WechatHttpMessageConverter() {

        super(new MediaType("text", "plain", Charset.forName("UTF-8")));
    }

    @Override
    protected boolean supports(Class<?> aClass) {
        return aClass.isAssignableFrom(Map.class);
    }

    @Override
    protected Map readInternal(Class<? extends Map> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
        return new ObjectMapper().readValue( httpInputMessage.getBody(), Map.class);
    }

    @Override
    protected void writeInternal(Map map, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException {

    }
}
